from pydantic import BaseModel
from datetime import datetime
from typing import Optional


class OperationLogBase(BaseModel):
    """
    操作日志基础模型
    """
    user_id: Optional[int] = None
    username: Optional[str] = None
    ip_address: Optional[str] = None
    operation_type: str
    operation_content: str
    success: int = 1
    error_message: Optional[str] = None


class OperationLogCreate(OperationLogBase):
    """
    创建操作日志模型
    """
    pass


class OperationLog(OperationLogBase):
    """
    操作日志响应模型
    """
    id: int
    operation_time: datetime
    # 为了前端显示需要，添加content字段作为operation_content的别名
    content: str = None
    
    def __init__(self, **data):
        super().__init__(**data)
        # 设置content为operation_content的值
        self.content = self.operation_content

    class Config:
        from_attributes = True  # Pydantic V2使用from_attributes替代orm_mode


class OperationLogQuery(BaseModel):
    """
    操作日志查询模型
    """
    user_id: Optional[int] = None
    username: Optional[str] = None
    operation_type: Optional[str] = None
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None
    success: Optional[int] = None
    page: int = 1
    page_size: int = 20